﻿cmake_minimum_required(VERSION 3.0.0)
project(nebulaWeaver VERSION 1.1.0 LANGUAGES C CXX)

# 设置版本号
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATH 0)
set(PROJECT_VERSION_TWEAK 0)
set(PROJECT_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${PROJECT_VERSION_TWEAK}")


# 设置dll版本号
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 1)
set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")

#添加版本信息
include(${PROJECT_SOURCE_DIR}/cmake/version.cmake)

#添加dll信息
include(${PROJECT_SOURCE_DIR}/cmake/dllVersion.cmake)


# 定义MYLIBRARY_EXPORTS宏，以便正确导出符号
if(WIN32)
    add_definitions(
        -DNEBULAWEAVER_SHARED_LIBRARY
        -DNEBULAWEAVER_COMPILE_LIBRARY
    )
endif()

# 添加选项
option(BUILD_SHARED_LIBS "Build shared libraries (ON/OFF)" OFF)

option(AUTO_ADD_INCLUDE "auto add include (ON/OFF)"  ON)

# 添加头文件
file(GLOB_RECURSE header_list ${CMAKE_CURRENT_SOURCE_DIR}/inc/*.h)

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../nebulaWeaverLib)

# 设置安装目录
set(INSTALL_DIR  ${PROJECT_SOURCE_DIR}/../install)


if(AUTO_ADD_INCLUDE)
    # 获取当前目录下所有文件夹
    function(get_subdirectories result current_dir)
        set(unmatched_subdirs "")
        set(matched_subdirs "")
        set(string2 "") #过滤
        file(GLOB SUBDIRS RELATIVE ${current_dir} ${current_dir}/*)
        foreach(subdir ${SUBDIRS})
            if(IS_DIRECTORY ${current_dir}/${subdir})
                set(found FALSE)
                foreach(dir IN LISTS string2)
                    if(subdir STREQUAL dir)
                        set(found TRUE)
                        list(APPEND matched_subdirs ${current_dir}/${subdir})  # 将匹配的文件夹添加到 matched_subdirs 列表中
                        break()  # 找到匹配项后退出内部循环
                    endif()
                endforeach()
                if(NOT found)
                    list(APPEND unmatched_subdirs ${current_dir}/${subdir})  # 将不匹配的文件夹添加到 unmatched_subdirs 列表中
                endif()
            endif()
        endforeach()
        set(${result} ${unmatched_subdirs} PARENT_SCOPE)
    endfunction()

    # 调用函数获取当前路径下的所有目录
    get_subdirectories(subdirs ${CMAKE_CURRENT_SOURCE_DIR}/inc)

    foreach(dir ${subdirs})
        include_directories(${dir})
    endforeach()
else()
    # 添加头文件路径
    include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/inc
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/basic
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/utils
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/thread
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/task
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/queue
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/scheduler
        ${CMAKE_CURRENT_SOURCE_DIR}/inc/threadpool
        )
endif()
# 添加源文件文件
#aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src ALL_SOURCE)
file(GLOB_RECURSE ALL_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)


if(BUILD_SHARED_LIBS)
    # 创建动态库
    add_library(${PROJECT_NAME}
            SHARED
            ${ALL_SOURCE}
            ${header_list}
            ${CMAKE_CURRENT_BINARY_DIR}/version.h
            ${CMAKE_CURRENT_BINARY_DIR}/dllVersion.rc
            )
    set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${VERSION_STRING} SOVERSION ${VERSION_MAJOR})
else()
    # 创建静态库
    add_library(${PROJECT_NAME}
            STATIC
            ${ALL_SOURCE}
            ${header_list}
            ${CMAKE_CURRENT_BINARY_DIR}/version.h
            ${CMAKE_CURRENT_BINARY_DIR}/dllVersion.rc
            )
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
    OUTPUT_NAME ${PROJECT_NAME}
)


# 3、cmake在构建一个新的target时，会尝试清理掉其他使用这个名字的库，
# 因此，在构建libhello.a时，就会清理掉libhello.so.
set_target_properties(${PROJECT_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1)


# 4、按照规则，动态库是应该包含一个版本号的，
# VERSION指代动态库版本，SOVERSION指代API版本。
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.1 SOVERSION 1)


# 定义需要安装的文件
install(FILES ${header_list} DESTINATION ${INSTALL_DIR}/include)
install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR}/lib)



## 拷贝多个动态库文件，直接将所有的动态库放到文件夹下，拷贝文件夹即可
#add_custom_command(TARGET ${PROJECT_NAME}_static POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_directory
#   ${CMAKE_CURRENT_SOURCE_DIR}/inc  #需要拷贝的文件夹
#   ${LIBRARY_OUTPUT_PATH}/inc )#拷贝到哪个目录

#add_custom_command(TARGET ${PROJECT_NAME}_static POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_if_different
#    "${CMAKE_CURRENT_SOURCE_DIR}/inc/basic/HObject.h"  #需要拷贝的文件
#    "${LIBRARY_OUTPUT_PATH}/include/HObject.h") #拷贝到哪个目录

#### 拷贝单个动态库文件
#add_custom_command(TARGET ${PROJECT_NAME}_static POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_if_different
#    "${CMAKE_CURRENT_SOURCE_DIR}/inc/utils/UtilsObject.h"  #需要拷贝的文件
#    "${LIBRARY_OUTPUT_PATH}/include/UtilsObject.h") #拷贝到哪个目录

#add_custom_command(TARGET ${PROJECT_NAME}_static POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_if_different
#    "${CMAKE_CURRENT_SOURCE_DIR}/inc/basic/HDefine.h"  #需要拷贝的文件
#    "${LIBRARY_OUTPUT_PATH}/include/HDefine.h") #拷贝到哪个目录

#add_custom_command(TARGET ${PROJECT_NAME}_static POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_if_different
#    "${CMAKE_CURRENT_SOURCE_DIR}/inc/basic/NebulaWeaverdll.h"  #需要拷贝的文件
#    "${LIBRARY_OUTPUT_PATH}/include/NebulaWeaverdll.h") #拷贝到哪个目录


#add_dependencies(${PROJECT_NAME}_static ${CMAKE_CURRENT_BINARY_DIR}/dllVersion.rc)
#add_dependencies(${PROJECT_NAME}_shared ${CMAKE_CURRENT_BINARY_DIR}/dllVersion.rc)
